home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Tools / Win95 Secrets / SETUP.Z / SIMONSEZ.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-19  |  3.3 KB  |  83 lines

  1. //==================================
  2. // SIMONSEZ - Matt Pietrek 1995
  3. // FILE: HOOKAPI.C
  4. //==================================
  5. #include <windows.h>
  6. #include <malloc.h>
  7. #include "hookapi.h"
  8.  
  9. // Make a typedef for the WINAPI function we're going to intercept
  10. typedef int (__stdcall *MESSAGEBOXPROC)(HWND, LPCSTR, LPCSTR, UINT);
  11.  
  12. MESSAGEBOXPROC PfnOriginalMessageBox;   // for storing original address
  13.  
  14. //
  15. // A special version of MessageBox that always prepends "Simon Sez: "
  16. // to the text that will be displayed.
  17. //
  18. int WINAPI MyMessageBox( HWND hWnd, LPCSTR lpText,
  19.                          LPCSTR lpCaption, UINT uType )
  20. {
  21.     int retValue;               // real MessageBox return value
  22.     PSTR lpszRevisedString;     // pointer to our modified string
  23.     
  24.     // Allocate space for our revised string - add 40 bytes for new stuff
  25.     lpszRevisedString = malloc( lstrlen(lpText) + 40 );
  26.  
  27.     // Now modify the original string to first say "Simon Sez: "
  28.     if ( lpszRevisedString )
  29.     {
  30.         lstrcpy(lpszRevisedString, "Simon Sez: ");
  31.         lstrcat(lpszRevisedString, lpText);
  32.     }
  33.     else                                    // If malloc() failed, just
  34.         lpszRevisedString = (PSTR)lpText;   // use the original string.
  35.     
  36.     // Chain on to the original function in USER32.DLL.
  37.     retValue = PfnOriginalMessageBox(hWnd,lpszRevisedString,lpCaption,uType);
  38.  
  39.     if ( lpszRevisedString != lpText )  // If we sucessfully allocated string
  40.         free( lpszRevisedString );      // memory, then free it.
  41.             
  42.     return retValue;    // Return whatever the real MessageBox returned
  43. }
  44.  
  45. int APIENTRY WinMain( HANDLE hInstance, HANDLE hPrevInstance,
  46.                         LPSTR lpszCmdLine, int nCmdShow )
  47. {
  48.     MessageBox(0, "MessageBox Isn't Intercepted Yet", "Test", MB_OK);
  49.     
  50.     // Intercept the calls that this module (TESTHOOK) makes to
  51.     // MessageBox() in USER32.DLL.  The function that intercepts the
  52.     // calls will be MyMessageBox(), above.
  53.  
  54.     PfnOriginalMessageBox = (MESSAGEBOXPROC) HookImportedFunction(
  55.                     GetModuleHandle(0),     // Hook our own module
  56.                     "USER32.DLL",           // MessageBox is in USE32.DLL
  57.                     "MessageBoxA",          // function to intercept
  58.                     (PROC)MyMessageBox);    // interception function
  59.  
  60.     if ( !PfnOriginalMessageBox )   // Make sure the interception worked
  61.     {
  62.         MessageBox(0, "Couldn't hook function", 0, MB_OK);
  63.         return 0;
  64.     }
  65.  
  66.     // !!!!!!!!!!!!!!!!!!!!!!!!  WARNING  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  67.     // When built with optimizations, the VC++ compiler loads a
  68.     // register with the address of MessageBoxA, and then makes all
  69.     // subsequent calls through it.  This can cause the MessageBox call
  70.     // below to not go through the Import Address table that we just patched.
  71.     // For this reason, the .MAK file for this program does not use the
  72.     // /O2 or /O1 switches.  This usually won't be a problem, but it
  73.     // was in this particularly simple program.  ACCKK!!!!
  74.  
  75.     // Call MessageBox again.  However, since we've now intercepted
  76.     // MessageBox, control should first go to our own function
  77.     // (MyMessageBox), rather than the MessageBox() code in USER32.DLL.
  78.  
  79.     MessageBox(0, "MessageBox Is Now Intercepted", "Test", MB_OK);
  80.  
  81.     return 0;
  82. }
  83.